/**
 * @apiDefine ErrorResponse
 * @apiError (请求错误) {String} err_message 错误信息描述
 */

/**
 * @apiVersion 1.0.0
 * @apiGroup 分类
 * @api {POST} api/category/delete 删除分类
 * @apiName api/category/delete
 * @apiExample {js} axios 例子:
 *  axios.post('http://39.105.38.34:8080/api/category/delete', {
 *    id: '分类ID',
 *    kind: '分类级别'
 *  });
 * @apiHeader {String} authorization 用户登录或注册成功时返回的 token
 * @apiParam {number} id 分类id
 * @apiParam {number} kind 1: 一级分类， 2: 二级分类
 * @apiSuccess (请求成功) {Null} data 删除成功后不返回数据
 * @apiuse  ErrorResponse
 */

 
const { query, del } = require('../../helper/db');
const { ok, door } = require('../../helper/util');

module.exports = async ctx => {
  var token = ctx.headers.authorization;
  var params = ctx.request.body;

  var user = await door(token);
  var { id, kind } = params;

  if (kind == 1) {
    var sql = ['select * from category where 1 = 1 and parentid = ?'];
    var values = [id];
    var categorylist = await query(sql.join(' '), values);

    categorylist.forEach(async (item) => {
      del('category', item.id);
    })
  }

  del('category', id);

  ctx.body = ok(null);
}